package com.simple.portal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.simple.base.dao.BaseDAO;
import com.simple.portal.dao.IOrganizationDAO;
import com.simple.portal.vo.OrganizationVO;
import com.simple.util.common.Constants;

@Repository("organizationDAO")
public class OrganizationDAOImpl extends BaseDAO implements IOrganizationDAO {

	@Override
	public OrganizationVO findById(final String id) {
		return super.get(OrganizationVO.class, id);
	}

	@Override
	public void save(final OrganizationVO organizationVO,final String operator) {
		OrganizationVO tempOrganizationVO = super.save(organizationVO);
		super.saveLogger(operator, tempOrganizationVO, tempOrganizationVO.getOrganizationId(),Constants.LOGGER_OPERATE_FLAG_ADD);
	}
	
	@Override
	public void update(final OrganizationVO organizationVO,final String type,final String operator) {
		super.update(organizationVO);
		super.saveLogger(operator, organizationVO, organizationVO.getOrganizationId(),type);
	}

	@Override
	public List<OrganizationVO> findAll() {
		return super.query(OrganizationVO.class);
	}

	@Override
	public void delete(final OrganizationVO organizationVO,final String operator) {
		super.delete(organizationVO);
		super.saveLogger(operator, organizationVO, organizationVO.getOrganizationId(), Constants.LOGGER_OPERATE_FLAG_DELETE);
	}

	@Override
	public void delete(final String id,final String operator) {
		OrganizationVO organizationVO = findById(id);
		super.delete(OrganizationVO.class, id);
		super.saveLogger(operator, organizationVO, organizationVO.getOrganizationId(), Constants.LOGGER_OPERATE_FLAG_DELETE);

	}
	
	@Override
	public List<OrganizationVO> findOrganizationVOList(final String userId){
		
		 	final StringBuffer sb = new StringBuffer(300);
	        sb.append( "select distinct org from GroupInfoVO g,OrganizationVO org  where g.organizationId = org.organizationId ");
	        sb.append(" and g.creator = ? ");
	        
			return   super.query(sb.toString(), userId);
		
	}

	@Override
	public List<OrganizationVO> findProviderList(String userId,
			String organizationType) {
		final StringBuffer sb = new StringBuffer(300);
		List<String> param = new ArrayList<String>();
        sb.append("select distinct org from ApplicationVO a,OrganizationVO org  where a.supplier = org.organizationId ");
        sb.append(" and org.creator = ? ");
        param.add(userId);
        sb.append(" and org.organizationType  = ?");
        param.add(organizationType);
              
		return super.query(sb.toString(), param);
	}

	@Override
	public List<OrganizationVO> findProvider(String applicationId) {
		final StringBuffer sb = new StringBuffer(300);
        sb.append("select distinct org from ApplicationVO a,OrganizationVO org  where a.supplier = org.organizationId ");
        sb.append(" and a.applicationId = ? ");
        return super.query(sb.toString(), applicationId);
	}


}
